#include-once
;Identification.au3

Global $cI_CompName = @ComputerName
Global $wbemFlagReturnImmediately = 0x10, _ ;DO NOT CHANGE
		$wbemFlagForwardOnly = 0x20 ;DO NOT CHANGE
;Global $generateComputerId_data

Func _generateComputerId()
	If _checkApi() = 0 Then
		Return 0
	EndIf
	Local $windowsPid = _getWindowsPid()
	Local $url = $geApiUrl & "/computer/add"
	Local $params = "name=" & _URLEncode(@ComputerName) & "&pid=" & _URLEncode($windowsPid)
	Local $sData = _apiCall($url,$params)
	Local $txt = BinaryToString($sData)
	Local $computerHash
	Local $array = StringSplit($txt, ",", 1)
	Local $i = 1
	While $i <= UBound($array) - 1
		Local $a = StringSplit($array[$i], ":")
		$key = StringRegExpReplace($a[1], '\W', "")
		$key = String($key)
		Local $val = StringRegExpReplace($a[2], '\W', "")
		If $key = "computerHash" Then
			$computerHash = $val
		EndIf
		$i = $i + 1
	WEnd
	If $computerHash Then
		debug("* Computer Name: " & @ComputerName)
		debug("* Windows PID  : " & $windowsPid)
		;Local $id = MD5Hash(@ComputerName & $windowsPid & $programUID, 2, True)
		debug("* Computer Hash: " & $computerHash)
		Return $computerHash
	Else
		debug("Could not get computer hash")
		Return
	EndIf
EndFunc

Func _getWindowsPid()
	Local $colItems, $objWMIService, $objItem, $windowsPid
	
	$objWMIService = ObjGet("winmgmts:\\" & $cI_Compname & "\root\CIMV2")
	$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
	
	If IsObj($colItems) Then
		For $objItem In $colItems
			Return $objItem.SerialNumber
		Next
	EndIf
EndFunc
